<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace addons\equip\model;

/**
 * 团队收益管理奖定时发放
 * Class TeamRevenueAward
 * @package addons\equip\model
 */
class TeamRevenueAward extends \web\common\model\BaseModel {

    protected function _initialize() {
        $this->tableName = 'team_revenue_award';
    }

    /**
     * 添加
     * @param $pid
     * @param $user_id
     * @param $release_balance_type
     * @param $amount
     * @param string $remark
     * @return bool
     */
    public function addData($pid, $user_id, $release_balance_type, $amount, $remark = ''){
        $add_data = [
            'pid' => $pid,
            'user_id' => $user_id,
            'release_balance_type' => $release_balance_type,
            'amount' => $amount,
            'create_time' => NOW_DATETIME,
            'remark' => $remark,
        ];
        $re = $this->add($add_data);
        if(!$re){
            return false;
        }
        return true;
    }

    /**
     * 获取记录总数
     * @param type $filter
     * @return int
     */
    public function getTeamRevenueAwardTotal($filter = '') {
        $userM = new \addons\member\model\MemberAccountModel();
        $sql = 'select count(*) as c from '.$this->getTableName() . ' as a left join '.$userM->getTableName().' b on  a.user_id=b.id  left join '.$userM->getTableName().' p on  a.pid=p.id  ';
        if($filter != ''){
            $sql = $sql. ' where ' . $filter;
        }
        $result = $this->query($sql);
        if (count($result) > 0)
            return intval($result[0]['c']);
        else
            return 0;
    }

    public function getTeamRevenueAwardList($pageIndex = -1, $pageSize = -1, $filter = '',$fileds='*', $order = 'id desc') {
        $userM = new \addons\member\model\MemberAccountModel();
        $sql = 'select '.$fileds.' from '.$this->getTableName() . ' as a left join '.$userM->getTableName().' b on  a.user_id=b.id left join '.$userM->getTableName().' p on a.pid=p.id ';
        if($filter != ''){
            $sql = $sql. ' where ' . $filter;
        }
        return $this->getDataListBySQL($sql, $pageIndex, $pageSize, $order);
    }

}
